package com.core.tag.impl;

import java.io.IOException;
import java.util.List;

import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

import com.core.util.DspUtil;
import com.dsp.bean.SelectTag;
import com.dsp.services.SelectTagService;

public class TagsInfo extends TagSupport {

	/**
	 * 
	 */
	private static final long serialVersionUID = -3011156514410267094L;

	/**
	 * 标签名称
	 */
	private String tagId;
	/**
	 * 页面显示名称
	 */
	private String htmlId;
	/**
	 * 数据库值
	 */
	private String htmlValue;
	
	/**
	 * 事件
	 */
	private String oAction;
	
	/**
	 * 是否包含空值
	 */
	private String isAll;

	/**
	 * @return the tagId
	 */
	public String getTagId() {
		return tagId;
	}

	/**
	 * @return the htmlId
	 */
	public String getHtmlId() {
		return htmlId;
	}

	/**
	 * @return the htmlValue
	 */
	public String getHtmlValue() {
		return htmlValue;
	}

	/**
	 * @param tagId
	 *            the tagId to set
	 */
	public void setTagId(String tagId) {
		this.tagId = tagId;
	}

	/**
	 * @param htmlId
	 *            the htmlId to set
	 */
	public void setHtmlId(String htmlId) {
		this.htmlId = htmlId;
	}

	/**
	 * @param htmlValue
	 *            the htmlValue to set
	 */
	public void setHtmlValue(String htmlValue) {
		this.htmlValue = htmlValue;
	}
	
	/**
	 * @return the oAction
	 */
	public String getoAction() {
		return oAction;
	}

	/**
	 * @param oAction the oAction to set
	 */
	public void setoAction(String oAction) {
		this.oAction = oAction;
	}

	/**
	 * @return the isAll
	 */
	public String getIsAll() {
		return isAll;
	}

	/**
	 * @param isAll the isAll to set
	 */
	public void setIsAll(String isAll) {
		this.isAll = isAll;
	}

	public int doStartTag() {
		JspWriter out = pageContext.getOut();
		List<SelectTag> list = this.getData();
		SelectTag tag = null;
		boolean flag = true;
		StringBuffer select = new StringBuffer(100);
		select.append("<select name=\"" + htmlId + "\" id=\""+ htmlId +"\" "+ oAction +">");
		if(isAll != null && !"".equals(isAll)){
			select.append("<option value=''>请选择</option>");
			flag = !flag;
		}
		String curr = "";
		for(int i = 0 ; i < list.size() ; i++){
			tag = list.get(i);
			if(flag){
				curr = tag.getTagDefault() == 1 ? "selected" : "";
			}
			select.append("<option value='").append(tag.getTagValue()).append("' ")
			.append(curr).append(" >")
			.append(tag.getTagName()).append("</option>");
		}
		select.append("</select>");
		try {
			out.print(select);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return SKIP_BODY;
	}
	
	/**
	 * 初始化数据
	 * @return
	 */
	private List<SelectTag> getData(){
		SelectTagService service = (SelectTagService)DspUtil.getBean("selectTagService");
		DetachedCriteria dc = DetachedCriteria.forClass(SelectTag.class);
		dc.add(Restrictions.eq("tagId", this.tagId));
		dc.addOrder(Order.asc("tagValue"));
		return service.likeBy(dc);
	}
}
